package com.peng.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("小方");
    }

    //配置Swagger Bean实例
    @Bean
    public Docket docket(Environment environment) {
        //获取配置文件
        Profiles profiles = Profiles.of("dev", "test");
        //判断当前swagger 是否在可用的生产环境下
        boolean b = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("小鹏")
                .apiInfo(apiInfo())
                //是否开启swagger
                // .enable(b)
                .select()
                //RequestHandlerSelectors 配置要扫描的接口
                //basePackage 指定扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.example"))
                //指定要扫描的路径
                //.paths(PathSelectors.ant("/peng/**"))
                .build();
    }

    private ApiInfo apiInfo() {
        Contact DEFAULT_CONTACT = new Contact("小鹏", "http://xiaopeng.com", "872071760@qq.com");
        return new ApiInfo("小鹏的Swagger文档",
                "小鹏在线学习Swagger",
                "1.0",
                "http:",
                DEFAULT_CONTACT,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
